<?php
namespace App\Services;

use App\Models\BaseConfig;

class BaseConfigService extends CommonService{
    
   
	/**
	 * 分页查询
	 * where([
    *			['status', '=', '1'],
    *			['subscribed', '<>', '1'],
    *	])
	 */
    function paginateInfo($where = null,$limit = 10){
    	try{
    		$result = BaseConfig::where($where)
    					->latest()
            			->paginate($limit);
         
            if(count($result) != 0){
            	return $this->show(1,'查询成功',$result);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    		
    }
    
    
   
    /**
     * 添加管理员
     */
    function addInfo($param = array()){
    	try{
    		$model = new BaseConfig();
    		foreach($param as $key => $value) {
                $model->$key = $value;
            }
          
            $result = $model->save();
            if($result){
            	return $this->show(1,'添加成功',$result);
            }else{
            	return $this->show(0,'添加失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    }
    
    /**
     * 更新管理员
     */
    function updateInfo($where = null,$param=[]){
    	try{
    		$result = BaseConfig::where($where)->update($param);
    		
            if($result){
            	return $this->show(1,'更新成功',$result);
            }else{
            	return $this->show(0,'更新失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    	
    	
    }
    
    
    /**
     * 查询单条管理员
     */
    function findInfo($where = null){
    	try{
    		$result = BaseConfig::where($where)->first();
            if($result){
            	return $this->show(1,'查询成功',$result);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    }
    
    
    /**
     *查询所有用户
     */
    function findAllInfo($where = null){
    	try{
    		$result = BaseConfig::where($where)->orderBy('sort', 'asc')->latest()->get();
            if($result){
            	return $this->show(1,'查询成功',$result);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    	return $result = BaseUser::get();
    }
    
    /**
     * 删除
     */
    function deleteInfo($where = null){
    	try{
    		$result = BaseConfig::where($where)->delete();
            if($result){
            	return $this->show(1,'删除成功',$result);
            }else{
            	return $this->show(0,'删除失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    }
    
    /**
     * key->value
     */
    /**
     *查询所有用户
     */
    function findAllKeyValue($where = null){
    	try{
    		$result = BaseConfig::where($where)->orderBy('sort', 'asc')->get();
            
            if($result){
            	$keyValue = [];
            	foreach($result as $key=>$value){
            		$keyValue[$value['item_key']]=$value['item_value'];
            	}
            	return $this->show(1,'查询成功',$keyValue);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}

    }
    
    /**
     * 根据建获取值
     */
    function findConfigKeyValue($value){
    	try{
    		$result = BaseConfig::where(array('item_key'=>$value))->value('item_value');
            if($result){
            	return $this->show(1,'查询成功',$result);
            }else{
            	return $this->show(0,'查询失败',$result);
            }
    	}catch(\Exception $e){
    		return $this->show(2,'系统错误',$e);
    	}
    }
}
